package com.ls.fw.open.support;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import javax.validation.constraints.Min;

import org.hibernate.validator.constraints.NotEmpty;

/**
 * 调用接口上下文
 * @author hc.tang
 *
 */
public class RequestContext {
	// 默认请求有效期30秒
	private static final int DEFAULT_EXPIRY_SECONDS = 30;
	
	@NotEmpty
	private String appId; // 接入方id
	@NotEmpty
	private String sign; // 数据签名
	@Min(1)
	private long timestamp; // 时间戳
	private String methodName; // 方法名称
	 
	private long serverStartTime = System.currentTimeMillis();
	private long serverEndTime;
	
	private boolean needSign = false; // 是否需要检查签名
	private boolean needTimeout = false;
	private int expirySeconds = DEFAULT_EXPIRY_SECONDS; // 有效期,单位秒
	
	private Map<String, String> paramsMap = new HashMap<String, String>();
	
	private Set<String> ignoreParamName; 
	private String[] allowRoles;
	private String[] denyRoles;
	
	public String getMethodName() {
		return methodName;
	}

	public void setMethodName(String methodName) {
		this.methodName = methodName;
	}

	public long getApiExecTime() {
		return serverEndTime - serverStartTime;
	}
	
	public long getTimestamp() {
		return timestamp;
	}

	public void setTimestamp(long timestamp) {
		this.timestamp = timestamp;
	}

	public long getServerStartTime() {
		return serverStartTime;
	}

	public void setServerStartTime(long serverStartTime) {
		this.serverStartTime = serverStartTime;
	}

	public long getServerEndTime() {
		return serverEndTime;
	}

	public void setServerEndTime(long serverEndTime) {
		this.serverEndTime = serverEndTime;
	}

	public void setParamsMap(Map<String, String> paramsMap) {
		this.paramsMap = paramsMap;
	}
  

	public Object getParamValue(String paramName) {
		return paramsMap.get(paramName);
	}

	public Map<String, String> getAllParams() {
		return paramsMap;
	}

	public String getAppId() {
		return appId;
	}

	public void setAppId(String appId) {
		this.appId = appId;
	}

	public String getSign() {
		return sign;
	}

	public void setSign(String sign) {
		this.sign = sign;
	}
	
	/**
	 * 获取某个参数
	 * @param name
	 * @return
	 */
	public Object getParam(String name) {
		return this.paramsMap.get(name);
	} 
	public boolean isNeedSign() {
		return needSign;
	}

	public void setNeedSign(boolean needSign) {
		this.needSign = needSign;
	}

	public Set<String> getIgnoreParamName() {
		return ignoreParamName;
	}

	public void setIgnoreParamName(Set<String> ignoreParamName) {
		this.ignoreParamName = ignoreParamName;
	}
 
	public int getExpirySeconds() {
		return expirySeconds;
	}

	public void setExpirySeconds(int expirySeconds) {
		this.expirySeconds = expirySeconds;
	}

	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append("RequestContext [appId=");
		builder.append(this.appId);
		builder.append(", sign=");
		builder.append(this.sign);
		builder.append(", timestamp=");
		builder.append(this.timestamp);
		builder.append(", methodName=");
		builder.append(this.methodName);
		builder.append(", serverStartTime=");
		builder.append(this.serverStartTime);
		builder.append(", serverEndTime=");
		builder.append(this.serverEndTime);
		builder.append(", needSign=");
		builder.append(this.needSign);
		builder.append(", expirySeconds=");
		builder.append(this.expirySeconds);
		builder.append(", paramsMap=");
		builder.append(this.paramsMap);
		builder.append(", ignoreParamName=");
		builder.append(this.ignoreParamName);
		builder.append("]");
		return builder.toString();
	}

	public String[] getAllowRoles() {
		return this.allowRoles;
	}

	public void setAllowRoles(String[] allowRoles) {
		this.allowRoles = allowRoles;
	}

	public String[] getDenyRoles() {
		return this.denyRoles;
	}

	public void setDenyRoles(String[] denyRoles) {
		this.denyRoles = denyRoles;
	}

	public boolean isNeedTimeout() {
		return needTimeout;
	}

	public void setNeedTimeout(boolean needTimeout) {
		this.needTimeout = needTimeout;
	}
	
}
